/**
 * Project Name:springmgw Maven Webapp
 * File Name:FormatXmlProcess.java
 * Package Name:com.geng.spring4.util
 * Date:2015年1月13日上午5:21:03
 * Copyright (c) 2015, 上海理想信息产业（集团） All Rights Reserved.
 *
*/

package com.geng.spring4.util;

import java.util.Date;
import java.util.List;

import com.geng.imr.model.NewsItem;

/**
 * ClassName:FormatXmlProcess <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2015年1月13日 上午5:21:03 <br/>
 * @author   GengHongChuang
 * @version  
 * @since    JDK 1.6
 * @see 	 
 */
public class FormatXmlProcess {
    /** 
     * 封装文字类的返回消息 
     * @param to 
     * @param from 
     * @param content 
     * @return 
     */  
    public String formatXmlAnswer(String to, String from, String content) {  
        StringBuffer sb = new StringBuffer();  
        Date date = new Date();  
        sb.append("<xml><ToUserName><![CDATA[");  
        sb.append(to);  
        sb.append("]]></ToUserName><FromUserName><![CDATA[");  
        sb.append(from);  
        sb.append("]]></FromUserName><CreateTime>");  
        sb.append(date.getTime());  
        sb.append("</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[");  
        sb.append(content);  
        sb.append("]]></Content><FuncFlag>0</FuncFlag></xml>");  
        return sb.toString();  
    }  
    public String formatXmlNewsAnswer(String to,String from,List<NewsItem> newsItems){
        StringBuffer sb = new StringBuffer();  
        Date date = new Date();  
        int count = newsItems.size();
        sb.append("<xml><ToUserName><![CDATA[");  
        sb.append(to);  
        sb.append("]]></ToUserName><FromUserName><![CDATA[");  
        sb.append(from);  
        sb.append("]]></FromUserName><CreateTime>");  
        sb.append(date.getTime());
        sb.append("</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>");  
        sb.append(count);
        sb.append("</ArticleCount><Articles>");
        for(NewsItem item:newsItems){
            sb.append("<item><Title><![CDATA[");
            sb.append(item.getTitle());
            sb.append("]]></Title><Description><![CDATA[");
            sb.append(item.getDescription());
            sb.append("]]></Description><PicUrl><![CDATA[");
            sb.append(item.getPicUrl());
            sb.append("]]></PicUrl><Url><![CDATA[");
            sb.append(item.getUrl());
            sb.append("]]></Url></item>");
        }
        sb.append("</Articles></xml>"); 
        return sb.toString();
    }

}
